#include "TrackCatcher.h"

#include <stdio.h>

TrackCatcher::TrackCatcher(LaserFinder* lf):
					lf(lf),
					has_next(false),
					empty_count_treshold(10) {
}

TrackCatcher::TrackCatcher(LaserFinder* lf, unsigned emptyCountTreshold):
					lf(lf),
					has_next(false),
					empty_count_treshold(emptyCountTreshold) {
}

bool TrackCatcher::next(Track& t) {
	if (hasNext()) {
		t = next_track;
		has_next = false;
		return true;
	} else {
		return false;
	}
}

bool TrackCatcher::hasNext() {
	if (has_next) {
		return true;
	}	
	
	finished = true;
	empty_count = 0;
	while(true) {
		IplImage* img = lf->getImage();
		if(!img) {
			return false;
		}
		int x = 0;
		int y = 0;
		if (lf->getPoint(img, &x, &y)){
			if (finished) {
				next_track.clear();
				finished = false;
			}
			next_track.addPoint(x, y);
			printf("%d x %d\n", x, y);
			empty_count = 0;
		} else {
			//printf("empty\n");
			empty_count++;
			if (!finished && empty_count > empty_count_treshold) {
				finished = true;
				has_next = true;
				return true;
			}
		}
	}
}
